<?php
namespace app\massage\model;

use app\BaseModel;
use think\facade\Db;

class CarAtvList extends BaseModel
{
    //定义表名
    protected $name = 'shequshop_car_atv_list';



    protected $append = [

        'content'

    ];


    /**
     * @author chenniang
     * @DataTime: 2021-09-15 14:31
     * @功能说明:获取活动内容
     */
    public function getContentAttr($value,$data){

        if(!empty($data['id'])){

            $content = Db::name('shequshop_car_atv_content_content')->where(['atv_id'=>$data['id']])->column('content_id');

            return array_values($content);
        }

    }





    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:04
     * @功能说明:添加
     */
    public function dataAdd($data){

        $data['create_time'] = time();

        if(isset($data['content'])){

            $content = $data['content'];

            unset($data['content']);
        }

        $res = $this->insert($data);

        if(!empty($content)){

            $id = $this->getLastInsID();

            $this->updateSome($id,$content,$data['uniacid']);
        }

        return $res;

    }


    /**
     * @author chenniang
     * @DataTime: 2021-09-23 18:06
     * @功能说明:根据活动时间修改活动状态
     */
    public function initAtv(){

        $this->where('atv_s_time','>',time())->update(['atv_status'=>1]);

        $this->where('atv_e_time','<',time())->update(['atv_status'=>3]);

        $this->where('atv_s_time','<',time())->where(['atv_status'=>1])->update(['atv_status'=>2]);

        return true;

    }


    /**
     * @author chenniang
     * @DataTime: 2021-09-15 11:22
     * @功能说明:
     */
    public function updateSome($id,$content,$uniacid){

        Db::name('shequshop_car_atv_content_content')->where(['atv_id'=>$id])->delete();

        if(!empty($content)){

            foreach ($content as $value){

                $insert = [

                    'uniacid' => $uniacid,

                    'content_id' => $value,

                    'atv_id' => $id
                ];

                Db::name('shequshop_car_atv_content_content')->insert($insert);
            }

        }

        return true;

    }


    /**
     * @author chenniang
     * @DataTime: 2021-10-20 10:02
     * @功能说明:判断报名内容是否在使用
     */
    public function atvContentIng($content_id){

        $dis[] = ['a.status','>',-1];

        $dis[] = ['b.content_id','=',$content_id];

        $data = $this->alias('a')
                ->join('shequshop_car_atv_content_content b','a.id = b.atv_id')
                ->where($dis)
                ->find();

        return $data;

    }



    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:05
     * @功能说明:编辑
     */
    public function dataUpdate($dis,$data){

        if(isset($data['content'])){

            $content = $data['content'];

            unset($data['content']);
        }

        $res = $this->where($dis)->update($data);

        if(!empty($content)){

            $id = $this->getLastInsID();

            $this->updateSome($data['id'],$content,$data['uniacid']);
        }
        return $res;

    }


    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:06
     * @功能说明:列表
     */
    public function dataList($dis,$page=10,$mapor=[]){

        $data = $this->where($dis)->order('id desc')->paginate($page)->toArray();

//        $data = $this->alias('a')
//                ->join('massage_service_user_list b','a.user_id = b.id')
//                ->where($dis)
//                ->where(function ($query) use ($mapor){
//                    $query->whereOr($mapor);
//                })
//                ->field('a.*,b.nickName')
//                ->group('a.id')
//                ->order('a.id desc')
//                ->paginate($page)
//                ->toArray();



        return $data;

    }


    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:43
     * @功能说明:
     */
    public function dataInfo($dis){

        $data = $this->where($dis)->find();

        return !empty($data)?$data->toArray():[];

    }







}